<?php
namespace Home\Controller;               /* 命名空间。表示当前类是Home模块下控制器类 */
use Common\Controller\BaseController;     /* 表示引入Common\Controller\BaseController命名空间便于直接使用 */

class IndexController extends BaseController {  /* 等同于 “class IndexController extends Common\Controller\BaseController”，即子类“IndexController.class.php”继承父类“BaseController.class.php”  */
	
	public $classify_list_top = array();
	//Index 每一个方法都要调用到的
	protected function _initialize(){    /* 申明函数_initialize()函数 */
		parent::_initialize();           /* 调用类_initialize的方法 */
		$listEmpty = "<div class=\"newscase\">暂时没有数据</div>";
		$this->assign("listEmpty",$listEmpty);

		//TOP 导航栏
		$_top_menu = M('classify')->field('id,name,subname,link')->where('status=1 and pid=0')->order('sort desc')->limit(11)->select();
		$this->assign('top_menu', $_top_menu);
		$this->classify_list_top = $this->getSubByKey($this->classify_list,'id');

		//FOOT 导航栏
		$_foot_menu = M('classify')->field('id,name,subname,link')->where('status=1 and pid=12')->order('sort desc')->limit(11)->select();
		$this->assign('foot_menu', $_foot_menu);
		if(session('userInfo.id') > 0){
			$user_id = session('userInfo.id');
			$info = M('students')->field('id,name,gh')->where(array('id' => $user_id))->find();
			$this->assign('info', $info);
		}
	}
	
	// 校园新闻
	private function _index_xyxw(){
		$_sql  = "select count(id) num,FROM_UNIXTIME(times,'%Y-%m-%d') rq,".
				"FROM_UNIXTIME(times,'%m') rq_m,FROM_UNIXTIME(times,'%d') rq_d ".
				"from app_news where status=1 group by FROM_UNIXTIME(times,'%Y-%m-%d') ".
				"order by times desc limit 5";
		$list = M()->query($_sql);
		foreach ($list as $_k => &$_v) {
			$_v['_list_forRQ'] = M('news')->field('id,pid,title,content,times,desc')
					->where("status=1 and FROM_UNIXTIME(times,'%Y-%m-%d') = '".$_v['rq']."'")
					->order('sort desc,id desc')->limit(3)->select();
		}
		$this->assign('IL_xyxw', $list);
		$this->assign("IL_xyxw_class",explode(",","na,nb,nc,nd,ne"));
	}
	/**
	 * Action WebSite首页
	 * @return [type] [description]
	 */
	public function index() {

		$this->_index_xyxw();


		$info = array(
					'IL_ssxx' => array('id' => '3', 'num' => 8),	// 赛事信息
					'IL_zsdt' => array('id' => '29', 'num' => 10),	//招生动态
					'IL_jxxx' => array('id' => '28', 'num' => 10),	//教学信息	
					'IL_jwxx' => array('id' => '27', 'num' => 10)	//教务信息
				);
		$indexList = array();
		foreach($info as $k=>$v){
			$list = M('news')->field('id,pid,title,times,desc')->where('status=1 and pid in (' . $v['id'] . ')')
							->order('sort desc,id desc')->limit($v['num'])->select();
			$indexList[$k] = $list;
		}
		$this->assign('indexList', $indexList);

		//轮播
		$info = array(
					'lb1' => array('id' => '1', 'num' => 4),	//首页轮播图-banner
					'lb2' => array('id' => '2', 'num' => 2),	//专题网站外链
					'lb3' => array('id' => '3', 'num' => 15),	//首页图片外链
					'lb4' => array('id' => '4', 'num' => 5),	//中医药竞赛管理
				);
		$indexSlideList = array();
		foreach($info as $k=>$v){
			$list = M('slide')->field('id,cid,name,pic')->where('status=1 and cid in (' . $v['id'] . ')')
							->order('sort desc,id asc')->limit($v['num'])->select();
			$indexSlideList[$k] = $list;
		}
		$this->assign('indexSlideList', $indexSlideList);		
	    $this->display();
	}

	/**
	 * 获取一级导航ID，当前栏目信息
	 */
	private function _get_classify_info($id){
		//获取当前分类信息
		$classify_info = $this->getInfo('id,pid,name,subname,pic,link', 'classify', 'id=' . $id);
		$this->assign('classify_info', $classify_info);

		//得到当前的一级导航，ID
		$_top_pid = $classify_info['pid'] > 0 ? $classify_info['pid'] : $id;
		$this->assign('top_pid', $_top_pid);
		
		return array("_top_pid"=>$_top_pid,"classify_info"=>$classify_info);
	}

	/**
	 * 获取当前一级导航信息 & 相应二级导航的信息
	 */
	private function _get_classify_list_sub($_top_pid){
		//获取分类及子分类信息
		$classify_list_sub = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (id= '.$_top_pid.' or pid=' . $_top_pid . ')', 'sort desc');
		$_top_pid_menuInfo = array();
		foreach ($classify_list_sub as $_k => &$_v) {
			if($_v['pid'] == 0){
				$_top_pid_menuInfo = $_v;
				unset($classify_list_sub[$_k]);
			}else{
				$_v['child_sub'] = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (pid=' . $_v['id'] . ')', 'sort desc');
				$_v['child_sub_num'] = count($_v['child_sub']);
			}
		}
		//设置当前所在一级导航，菜单信息
		$this->assign('top_pid_menuInfo', $_top_pid_menuInfo);
		//二级菜单信息
		$this->assign('classify_list_sub', $classify_list_sub);
		return $classify_list_sub;
	}

	/**
	 * Action 新闻列表页
	 * @return [type] [description]
	 */
	public function lists(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取一级导航ID，当前栏目信息
			$_get_info = $this->_get_classify_info($id);
			$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
			$this->assign('_top_pid', $_top_pid);
			$classify_info = $_get_info['classify_info'];	//获取当前分类信息
			if ($classify_info){
				//获取子菜单
				$classify_list_sub = $this->_get_classify_list_sub($_top_pid);
				//获取当前分类及子分类新闻
				if($classify_info['pid'] == 0){
					foreach ($classify_list_sub as $key => $value) {
						$ids[] = $value['id'];
					}
					if(!$ids){
						$ids[] = $id;
					}
				}else{
					$ids[] = $id;
				}
				$news_list = $this->getList('id,pid,title,pic,content,times,view,desc', 'news', 'status=1 and pid in (' . implode(',', $ids) . ')', 'sort desc,times desc', true,4);
				$this->assign('news_list', $news_list);
				$this->display();
			}
		}
	}

	/**
	 * Action 子站点
	 * @return [type] [description]
	 */
	public function subset(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取一级导航ID，当前栏目信息
			$_get_info = $this->_get_classify_info($id);
			$classify_info = $_get_info['classify_info'];	//获取当前分类信息

			if ($classify_info){
				//获取子菜单
				$classify_list_sub = $this->_get_classify_list_sub($id);
				$_list_num = 5;
				$indexList = array();
				if(count($classify_list_sub) > 1){
					foreach($classify_list_sub as $v){
						$list = M('news')->field('id,pid,title,times,desc')->where('status=1 and pid in (' . $v['id'] . ')')
							->order('sort desc,id desc')->limit($_list_num)->select();
						$indexList[] = array(
											"sub_info"=>$v,
											"sub_data"=>$list
										);
					}
					$this->assign('indexList', $indexList);
					$this->display("subset");
				}else{
					$this->assign('indexList', $indexList);
					$this->display("subsetOne");
				}
			}
		}
	}

	/**
	 * Action 具体新闻页
	 * @return [type] [description]
	 */
	public function news(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取当前新闻信息
			$news_info = $this->getInfo('id,pid,title,times,view,content', 'news', 'status=1 and id=' . $id);
			if($news_info){
				//更新浏览数
				M('news')->where(array('id' => $id))->setInc('view');
				$this->assign('news_info', $news_info);


				//获取一级导航ID，当前栏目信息
				$_get_info = $this->_get_classify_info($news_info['pid']);
				$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
				//获取当前分类及子分类
				$this->_get_classify_list_sub($_top_pid);
				$this->display();
			}
		}
	}

	/**
	 * Action 站内搜索
	 * @return [type] [description]
	 */
	public function reserch(){
		//设置当前所在一级导航，菜单信息
		$_top_pid_menuInfo = array('pid' => 0, 'name' => '赛事搜索', 'subname' => 'RESERCH');
		$this->assign('top_pid_menuInfo', $_top_pid_menuInfo);

		$this->assign('classify_info', array('pid' => 0, 'name' => '赛事搜索', 'subname' => 'RESERCH'));

		$keyword = I('get.keyword', 0, 'strval');
		$_where = "status=1 and pid = 3";
		if($keyword){
			$_where .= " and (title like "%' . $keyword . '%" or content like "%' . $keyword . '%")";
		}
		//获取当前分类新闻
		$news_list = $this->getList('id,title,times,view,desc', 'news', $_where, 'sort desc,times desc', true);
		$this->assign('news_list', $news_list);
		$this->display('reserch');
	}

	//竞赛系统 - 信息查询
	public function reserchone(){
		//设置当前所在一级导航，菜单信息
		$_top_pid_menuInfo = array('pid' => 0, 'name' => '信息查询', 'subname' => 'RESERCH');
		$this->assign('top_pid_menuInfo', $_top_pid_menuInfo);

		$this->assign('classify_info', array('pid' => 0, 'name' => '信息查询', 'subname' => 'RESERCH'));

		$keyword = I('get.keyword', 0, 'strval');
		
			//获取当前分类新闻
		$news_list = $this->getList('id,title,times,view,desc', 'news',
						null, 'sort desc,times desc', true);
		$this->assign('news_list', $news_list);
		$this->display('reserchone');
	}
	/**
	 * 赛事信息列表页
	 * @return [type] [description]
	 */
	public function sslists(){
		 if(IS_POST){
				$name = I('post.name');
				$gh = I('post.gh');
				$info_login = M('js_bmsq')->field('id,gid,name')->where(array('name' => $name))->find();
				$info = M('students')->field('id,gh,name')->where(array('status' => 1, 'name' => $name))->find();
				if($info['gh'] === $gh){
					session('userInfo', array('id' => $info['id'], 'name' => $name,'gid' => $info['gid']));
					M('students')->where(array('id' => $info['id']))->save(array('last_login_time' => time(), 'last_login_ip' => $ip));
					//M('admin_login_log')->add(array('name' => $name, 'times' => time(), 'ip' => $ip));
                    $this->returnResult(true, '登录成功', U('Home/Index/sslistsone'));
				}else{
					//M('admin_login_log')->add(array('name' => $name, 'times' => time(), 'ip' => $ip, 'status' => 0, 'pwd' => $pwd));
					$this->returnResult(false, '姓名或学号错误');
				}
				
			}else{
		         $id = 3;
				 if($id > 0){
					//获取一级导航ID，当前栏目信息
					$_get_info = $this->_get_classify_info($id);
					$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
					$this->assign('_top_pid', $_top_pid);
					$classify_info = $_get_info['classify_info'];	//获取当前分类信息
					if ($classify_info){
						//获取子菜单
						$classify_list_sub = $this->_get_classify_list_sub($_top_pid);
						//获取当前分类及子分类新闻
						if($classify_info['pid'] == 0){
							foreach ($classify_list_sub as $key => $value) {
								$ids[] = $value['id'];
							}
							if(!$ids){
								$ids[] = $id;
							}
						}else{
							$ids[] = $id;
						}
						$news_list = $this->getList('id,pid,title,pic,content,times,view,desc', 'news', 'status=1 and pid in (' . implode(',', $ids) . ')', 'sort desc,times desc', true,4);
						$this->assign('news_list', $news_list);
						$this->display();
					}
				  }
	            }
	        }

	    public function sslistsone(){
	    	if(IS_POST){ 
                session_destroy();  
				$this->returnResult(true,'退出成功',U('Home/Index/sslists'));
		     }
			$user_id=session('userInfo.id');
            $_user_list=array();
			$list_bm =  $this->getList('news_id', 'js_bmsq', array('gid' => $user_id));
			foreach ($list_bm as  $_v) {
				$_user_list[]=$_v['news_id'];
			}
			$this->assign('user_list', $_user_list);
			$id = 3;
			if($id > 0){
			//获取一级导航ID，当前栏目信息
			$_get_info = $this->_get_classify_info($id);
			$_top_pid = $_get_info['_top_pid'];				//得到当前的一级导航，ID
			$this->assign('_top_pid', $_top_pid);
			$classify_info = $_get_info['classify_info'];	//获取当前分类信息
			if ($classify_info){
				//获取子菜单
				$classify_list_sub = $this->_get_classify_list_sub($_top_pid);
				//获取当前分类及子分类新闻
				if($classify_info['pid'] == 0){
					foreach ($classify_list_sub as $key => $value) {
						$ids[] = $value['id'];
					}
					if(!$ids){
						$ids[] = $id;
					}
				}else{
					$ids[] = $id;
				}
				$news_list = $this->getList('id,pid,title,js_cslb_id,times', 'news', 'status=1 and pid in (' . implode(',', $ids) . ')', 'sort desc,times desc', true,4);
				$this->assign('news_list', $news_list);
				$this->display();
			}
	        }
	    }
	   public function enlist(){
			$id=session('userInfo.id');
			if(IS_POST){ 
			 	if(!$_POST['pic']){
				$this->returnResult(false,"报名必须上传照片");
			}
			$info = $this->getInfo('id,name,sex,f3,f8,f6,dep_xb_name,dep_zy_name', 'students', array('id' => $id));
			$news_info = $this->getInfo('id,js_cslb,js_cslb_id,js_csxm,js_csxm_id', 'news', array('id' => I('get.ori_id', 0, 'intval')));
			$this->assign('news_info', $news_info);
			$_POST['name']=$info['name'];
			$_POST['gid']=$info['id'];
			$_POST['cslb_id']=$news_info['js_cslb_id'];
			$_POST['cslb_name']=$news_info['js_cslb'];
			$_POST['csxm_id']=$news_info['js_csxm_id'];
			$_POST['csxm_name']=$news_info['js_csxm'];
			$_POST['news_id']=$news_info['id'];
			if($info['sex']=='男'){
				 $_POST['sex']=1;
			}else{
				 $_POST['sex']=2;
			}
			$_POST['csnj']=$info['f3'];
			$_POST['sfz']=$info['f6'];
			$_POST['mz']=$info['f8'];
			$_POST['csxb']=$info['dep_xb_name'];
			$_POST['cszy']=$info['dep_zy_name'];
			$result = $this->updateData($_POST,'js_bmsq');
			$this->returnResult($result,'报名成功',U('Home/Index/sslistsone'));
			}else{
				$info = $this->getInfo('id,name,sex,f3,f8,f6,dep_xb_name,dep_zy_name', 'students', array('id' => $id));
				$this->assign('info', $info);
				$news_info = $this->getInfo('id,title,js_cslb,js_csxm', 'news', array('id' => I('get.ori_id', 0, 'intval')));
				$this->assign('news_info', $news_info);
				$this->display();
			}
		}
		public function enlistone(){
			$_news_id = I('get.news_id', 0, 'intval');
			$_user_id = session('userInfo.id');
			$info= $this->getInfo('*', 'js_bmsq',array('gid' => $_user_id,'news_id'=>$_news_id));
			$this->assign('info', $info);

		    $news_info = $this->getInfo('id,title', 'news', array('id' => $_news_id));
		    $this->assign('news_info', $news_info);
		    $this->display();
		}
     }

    
